<template>
	<section class="vbox">
		<section class="scrollable padder">
			<section class="panel panel-default m-t-sm">
				<header class="panel-heading">
					视频 |音频 | 图片 | 图书
					<ul class="nav nav-tabs pull-right no-border">
						<li class="m-r-md m-t-xs">
							<el-select v-model="mediaType_q" clearable placeholder="媒体类型" @change="changeMediaType_q">
								<el-option v-for="item in mediaTypes" :key="item.code" :label="item.name" :value="item.code">
								</el-option>
							</el-select>
						</li>
						<li class="m-r-sm m-t-xs">
							<el-input placeholder="输入标题关键字" v-model="keywords" clearable>
								<template #append>
									<el-button slot="append" type="success" icon="icon-magnifier icon" @click="query(1)"></el-button>
								</template>
							</el-input>
						</li>
						<li class="m-r-md m-t-xs">
							<el-dropdown trigger="click" @command="openMediaForm">
								<el-button type="success">
									<i class="fa fa-plus-circle"></i> 新增
								</el-button>
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item icon="icon-social-youtube icon text-primary" command="video">视频</el-dropdown-item>
										<el-dropdown-item icon="fa fa-music text-success" command="audio">音频</el-dropdown-item>
										<el-dropdown-item icon="fa fa-picture-o text-info" command="picture">图片</el-dropdown-item>
										<el-dropdown-item icon="icon icon-notebook text-success-dk" command="book">图书</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>
						</li>
					</ul>
				</header>
				<div class="panel-body">
					<el-table size="medium" :data="mediaList" style="width: 100%" border>
						<el-table-column fixed type="index" width="auto" :index="indexMethod">
						</el-table-column>
						<el-table-column prop="title" label="标题">
						</el-table-column>
						<el-table-column prop="author" label="作者">
						</el-table-column>
						<el-table-column prop="mediaType" label="媒体类型" :formatter="formatterMediaType">
						</el-table-column>
						<el-table-column prop="createUser" label="创建人">
						</el-table-column>
						<el-table-column prop="updateTime" label="更新时间">
						</el-table-column>
						<el-table-column align="center" label="发布" width="80">
							<template v-slot="scope">
								<el-switch v-model="scope.row.publish" active-color="#13ce66" @change="publishMedia($event, scope.row)">
								</el-switch>
							</template>
						</el-table-column>
						<el-table-column align="center" label="链接是否有效" prop="isValidUrl">
						</el-table-column>
						<el-table-column align="center" label="操作">
							<template v-slot="scope">
								<el-button type="text" @click="openMediaForm(scope.row.mediaType, scope.row.id)">编辑</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="deleteMedia(scope.row)">删除</el-button>
								<el-divider direction="vertical"></el-divider>
								<el-button type="text" @click="showDetail(scope.row.mediaType, scope.row.id)">预览</el-button>
							</template>
						</el-table-column>
					</el-table>

					<el-pagination background layout="prev, pager, next" :pager-count="5" :total="total" :page-size="pageSize"
					 @current-change="query">
					</el-pagination>
				</div>
			</section>
		</section>
	</section>
</template>

<script>
	export default {
		name: 'media',
		components: {

		},
		data() {
			return {
				mediaTypes: [],
				mediaList: [],
				total: 0, // 记录总条数
				pageSize: 10, // 每页显示条数
				pageNo: 1, // 当前的页数
				keywords: '',
				mediaType_q: '',
				publish_q: ''
			}
		},
		mounted() {
			this.$api.sysDict.queryDictItems("media_type").then(data => {
				this.mediaTypes = data;
			});

			this.query(1);
		},
		methods: {
			indexMethod(index) {
				return this.pageSize * (this.pageNo - 1) + index + 1;
			},
			formatterMediaType(row, column, cellValue, index) {
				return this.parseSysDict(cellValue, this.mediaTypes);
			},
			query(pageNo) {
				this.pageNo = null == pageNo ? 1 : pageNo;
				this.$api.media.query({
					title: this.keywords,
					mediaType: this.mediaType_q,
					publish: this.publish_q,
					pageSize: this.pageSize,
					pageNo: this.pageNo
				}).then(data => {
					this.mediaList = data.rows;
					this.pageNo = data.pageNo;
					if (1 == this.pageNo) {
						this.total = data.total;
					}
				});
			},
			openMediaForm(mediaType, id) {
				if (Number(id)) {} else {
					id = 0;
				}

				this.$router.push({
					name: mediaType + 'Form',
					params: {
						id: id
					}
				});
			},
			deleteMedia(media) {
				this.$confirm('您确定要执行删除操作吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api[media.mediaType].delete(media.id).then(data => {
						this.query(this.pageNo);
					});
				});
			},
			showDetail(mediaType, id) {
				console.debug('id', id);
				this.$router.push({
					path: '/' + mediaType + '/' + id,
					params: {
						// action: 'add'
					}
				});
			},
			publishMedia(e, item) {
				this.$api[item.mediaType].update(item);
			},
			changePublish_q(publishState) {
				this.publish_q = publishState;
				this.query(1);
			},
			changeMediaType_q(mediaType) {
				this.mediaList = [];
				this.query(1);
			},
			parseSysDict(key, data) {
				// console.debug('filter key:', key);
				var name = "其他";
				// console.debug('data:', data);
				for (let i = 0; i < data.length; i++) {
					// console.debug('data[i].code:', data[i].code);
					if (data[i].code == key) {
						name = data[i].name;
						// console.debug('name:', name);
						break;
					}
				}
				// console.debug('filter name:', name);
				return name;
			}
		}
	}
</script>

<style>
	.th-btn {
		border: 1px solid transparent;
		background-color: transparent;
		padding: 0;
	}
</style>
